/*
 * 类名称:AdminCustomerServiceImpl.java
 * 包名称:com.unimall.graded.admin.api.customer
 *
 * 修改履历:
 *     日期                       修正者        主要内容
 *     2020-03-17 09:33:10        lin     初版做成
 *
 * Copyright (c) 2019-2019 浵浵科技
 */
package com.unimall.graded.admin.api.customer;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.iotechn.unimall.core.exception.AdminServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.core.exception.ServiceException;
import com.unimall.graded.data.domain.CustomerDO;
import com.unimall.graded.data.mapper.CustomerMapper;
import com.iotechn.unimall.data.model.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.apache.ibatis.session.RowBounds;

import java.util.Date;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * Admin分销用户ServiceImpl实现类
 *
 * @author lin
 * @date 2020-03-17 09:33:10
 */
@Service
public class AdminCustomerServiceImpl implements AdminCustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer create(Long adminId, CustomerDO customerDO) throws ServiceException {
        if (customerDO == null) {
            throw new AdminServiceException(ExceptionDefinition.REQUEST_CORE_PARAM_CHECK_FAILED);
        }
        if (customerDO.getUserId() == null) {
            throw new AdminServiceException("unimall的用户id不能为空", ExceptionDefinition.PARAM_IS_NULL_CODE);
        }
        if (customerMapper.selectCount(new EntityWrapper<CustomerDO>().eq("user_id", customerDO.getUserId())) > 0) {
            throw new AdminServiceException("unimall的用户id数据库中已存在", ExceptionDefinition.PARAM_IS_ALREADY_EXIST_CODE);
        }
        Date now = new Date();
        customerDO.setId(null);
        customerDO.setGmtCreate(now);
        customerDO.setGmtUpdate(now);
        return customerMapper.insert(customerDO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(Long adminId, Long id) throws ServiceException {
        return customerMapper.delete(new EntityWrapper<CustomerDO>()
                .eq("id", id)) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(Long adminId, CustomerDO customerDO) throws ServiceException {
        if (customerDO == null) {
            throw new AdminServiceException(ExceptionDefinition.REQUEST_CORE_PARAM_CHECK_FAILED);
        }
        if (customerDO.getUserId() == null) {
            throw new AdminServiceException("unimall的用户id不能为空", ExceptionDefinition.PARAM_IS_NULL_CODE);
        }

        Date now = new Date();
        customerDO.setGmtUpdate(now);
        return customerMapper.updateById(customerDO) > 0;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public CustomerDO info(Long adminId, Long id) throws ServiceException {

        if (id == null) {
            throw new AdminServiceException(ExceptionDefinition.REQUEST_CORE_PARAM_CHECK_FAILED);
        }

        return customerMapper.selectById(id);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Page<CustomerDO> list(Long adminId,
                                 Long id,
                                 Long userId,
                                 Long parentId,
                                 String realname,
                                 Integer page,
                                 Integer limit) throws ServiceException {
        Wrapper<CustomerDO> wrapper = new EntityWrapper<CustomerDO>();
        wrapper.orderBy("id", false);
        if (id != null) {
            wrapper.eq("id", id);
        }
        if (userId != null) {
            wrapper.eq("user_id", userId);
        }
        if (parentId != null) {
            wrapper.eq("parent_id", parentId);
        }
        if (!StringUtils.isEmpty(realname)) {
            wrapper.eq("realname", realname);
        }
        List<CustomerDO> CustomerDOs = customerMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
            Integer count = customerMapper.selectCount(wrapper);
        return new Page<CustomerDO>(CustomerDOs, page, limit, count);
    }
}